﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CodeWork.Library.Web;
using CodeWork.BLL;

public partial class public_Register : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = "Registration";
        uccCountry.DataBind();
        SetFieldColors();
        InitControls();
    }

    private void InitControls()
    {
        ibtCreateAccount.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/images/createAccountButton.png";
        ibtCancel.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/images/registerCancel.png";
    }

    private void SetFieldColors()
    {
        if (!IsPostBack)
        {
            System.Drawing.Color dimColor = System.Drawing.Color.FromArgb(161, 161, 161);
            tbxEmail.ForeColor = dimColor;
            tbxFirstName.ForeColor = dimColor;
            tbxLastName.ForeColor = dimColor;
        }
        else
        {
            System.Drawing.Color dimColor = System.Drawing.Color.FromArgb(161, 161, 161);
            System.Drawing.Color trueColor = System.Drawing.ColorTranslator.FromHtml("#575757");
            
            if(tbxEmail.Text == "sample@domain.com")
                tbxEmail.ForeColor = dimColor;
            else
                tbxEmail.ForeColor = trueColor;

            if (tbxFirstName.Text == "First Name")
                tbxFirstName.ForeColor = dimColor; 
            else
                tbxFirstName.ForeColor = trueColor;

            if (tbxLastName.Text == "Last Name")
                tbxLastName.ForeColor = dimColor;
            else
                tbxLastName.ForeColor = trueColor;
        }
    }

    protected void validateTerms(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = cbxAgreeTerms.Checked;
    }

    protected void Captcha_Validate(object sender, ServerValidateEventArgs e)
    {
        e.IsValid = uccCaptcha.IsValid;
        cuvCaptcha.ErrorMessage = uccCaptcha.ErrorMessage;
    }

    protected void CheckUserId(object sender, EventArgs e)
    {
        bool available = UserManager.CheckUserIdAvailability(tbxUserId.Text);

        if (available)
        {
            imgAvailability.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/images/check.jpg";
            imgAvailability.ToolTip = "Available";
        }
        else
        {
            imgAvailability.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/images/cross.jpg";
            imgAvailability.ToolTip = "Not Available";
        }

        imgAvailability.Visible = true;
    }

    protected void CreateAccount_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            if (UserManager.RegisterSiteuser(tbxUserId.Text, uccPassword.Password, tbxEmail.Text, tbxFirstName.Text,
                tbxLastName.Text, uccDateOfBirth.Date, uccCountry.SelectedValue, tbxPostalCode.Text, tbxPhone.Text,
                tbxSecurityQuestion.Text, tbxSecurityAnswer.Text))
            {
                //SessionManager.StoreValue(SessionManager.SessionKeys.RegistrationEmail, tbxEmail.Text);
                Response.Redirect("registrationcomplete.aspx");
            }
            else
                ResponseMessage = "Registration Failed";
        }
        imgAvailability.Visible = false;
    }

    protected void Cancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/default.aspx");
    }
}
